/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */
define([ 'adminapp', 'backstretch'], function ($app) {
    var Login = function () {

        var handleLogin = function () {
            $('#username').focus();
            $('.login-form').validate({
                errorElement: 'span', //default input error message container
                errorClass  : 'help-block', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                rules       : {
                    username: {
                        required: true
                    },
                    password: {
                        required: true
                    },
                    remember: {
                        required: false
                    }
                },

                messages: {
                    username: {
                        required: "用户账号不能为空."
                    },
                    password: {
                        required: "密码不能为空."
                    }
                },

                invalidHandler: function (event, validator) { //display error alert on form submit
                    $('.alert-danger', $('.login-form')).show();
                    $('#username').focus();
                },

                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.form-group').addClass('has-error'); // set error class to the control group
                },

                success: function (label) {
                    label.closest('.form-group').removeClass('has-error');
                    label.remove();
                },

                errorPlacement: function (error, element) {
                    error.insertAfter(element.closest('.input-icon'));
                },

                submitHandler: function (form) {
                    form.submit();
                }
            });

            $('.login-form input').keypress(function (e) {
                if (e.which == 13) {
                    if ($('.login-form').validate().form()) {
                        $('.login-form').submit();
                    }
                    return false;
                }
            });
        };


        return {
            //main function to initiate the module
            init: function () {
                handleLogin();
                $.backstretch([
                        g.ctx + "static/admin/img/bg/1.jpg",
                        g.ctx + "static/admin/img/bg/2.jpg",
                        g.ctx + "static/admin/img/bg/3.jpg",
                        g.ctx + "static/admin/img/bg/4.jpg"
                ], {
                    fade    : 1000,
                    duration: 8000
                });
            }

        };

    }();

    Login.init();
    return {};
});